package com.nyist.myticket.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.nyist.myticket.biz.entity.User;
import com.nyist.myticket.biz.entity.dto.UserDto;
import com.nyist.myticket.biz.entity.vo.TbUserVo;
import com.nyist.myticket.biz.service.TbUserService;
import com.nyist.myticket.core.enums.MsgCodeEnum;
import com.nyist.myticket.core.utils.BeanConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nyist.myticket.core.base.BaseController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangxiaogen
 * @since 2021-01-05
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class TbUserController extends BaseController<TbUserVo, UserDto> {

    @Autowired
    private TbUserService tbUserService;

    /*@RequestMapping(value = "/createUser",method = RequestMethod.POST)
    public TbUser createUser(@RequestBody TbUser tbUser) {
        TbUser user = this.checkHandle(tbUser);
        if (user != null) {
            tbUserService.saveUser(tbUser);
        }
        return tbUser;
    }

    private TbUser checkHandle(TbUser tbUser) {

        if (tbUser.getMail() != null) {
            return tbUser;
        }
        return new TbUser();
    }*/

    @Override
    public TbUserVo checkHandle(TbUserVo tbUserVo, UserDto in) {
        tbUserVo.setCode("200");
        tbUserVo.setMsg("成功!");
        if (in.getMail().isEmpty()) {
            tbUserVo.setCode("101");
            tbUserVo.setMsg("mail不能为空");
        }
        return tbUserVo;
    }

    @RequestMapping(value = "/createUser",method = RequestMethod.POST)
    public TbUserVo createUSer(@RequestBody User user) {
        TbUserVo tbUserVo = this.checkHandle(new TbUserVo(), BeanConvertUtil.covertBean(user, UserDto.class));
        if (MsgCodeEnum.SUCCESS.getCode().equals(tbUserVo.getCode())) {
            tbUserService.saveUser(user);
            tbUserVo.setData(user);
        }
        return tbUserVo;
    }

    @RequestMapping("/queryUser")
    public TbUserVo queryUser(@RequestBody User tbUser) {
        TbUserVo tbUserVo = this.checkHandle(new TbUserVo(), BeanConvertUtil.covertBean(tbUser, UserDto.class));
        if (MsgCodeEnum.SUCCESS.getCode().equals(tbUserVo.getCode())) {
            User user = tbUserService.queryUserById(tbUser.getId());
            tbUserVo.setData(user);
        }
        return tbUserVo;
    }

    @RequestMapping("/queryUsers")
    public TbUserVo queryUsers(@RequestBody User tbUser) {
        // TbUserVo tbUserVo = this.checkHandle(new TbUserVo(), BeanConvertUtil.covertBean(tbUser, UserDto.class));
        // if (MsgCodeEnum.SUCCESS.getCode().equals(tbUserVo.getCode())) {
            List<User> users = tbUserService.listUser(tbUser);
            log.info("users...{}", JSONObject.toJSONString(users));
            // tbUserVo.setData(users);
        // }
        return null;
    }


    /*@RequestMapping(value = "/queryValue",method = RequestMethod.POST)
    public String queryValue(String key) {
        logger.info("===============准备获取 redis 的信息==============");
        String value = RedisUse.getValue(key);
        logger.info("===============创建任务成功,信息:{}", JSON.toJSONString(key));
        return value;
    }*/

    /*@RequestMapping(value = "/queryValue/{key}")
    public String queryValue(@PathVariable String key) {
        logger.info("===============准备获取 redis 的信息==============");
        String value = RedisUse.getValue(key);
        logger.info("===============创建任务成功,信息:{}", JSON.toJSONString(key));
        return value;
    }*/


}

